<template>
  <!--  小程序管理-->
  <div class="app-container">
    <el-form size="mini" :inline="true" :model="searchForm" @keyup.enter.native="getDataList()">
      <el-form-item label="小程序名称：">
        <el-input clearable placeholder="" style="width: 300px" v-model="searchForm.name"></el-input>
      </el-form-item>
      <el-form-item label="创建人：">
        <el-input clearable placeholder="" style="width: 300px" v-model="searchForm.created_user"></el-input>
      </el-form-item>
      <el-form-item label="创建时间：">
        <el-date-picker end-placeholder="结束日期" range-separator="至" start-placeholder="开始日期" type="daterange" v-model="searchForm.date" value-format="yyyy-MM-dd"> </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" @click="getDataList()" icon="el-icon-search" type="primary">查询</el-button>
        <el-button size="mini" @click="refDataList()" icon="el-icon-refresh" type="warning">重置</el-button>
      </el-form-item>
    </el-form>
    <div style="margin-bottom: 20px; text-align: right">
      <el-button size="mini" @click="addOrUpdateHandle()" type="primary">新增</el-button>
      <el-button size="mini" @click="handleDel()" type="danger">删除 </el-button>
    </div>
    <el-table :data="dataList" size="mini" border style="width: 100%" v-loading="dataListLoading" ref="multipleTable" @selection-change="handleSelectionChange">
      >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column align="center" header-align="center" label="编号" prop="typeName" width="80">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" header-align="center" label="小程序LOGO" prop="logo">
        <template slot-scope="scope">
          <span @click="handlePictureCardPreview('https://images.weserv.nl/?url=' + scope.row.logo)"><img :src="'https://images.weserv.nl/?url=' + scope.row.logo" alt="" width="100" height="100" /></span>
        </template>
      </el-table-column>
      <el-table-column align="center" header-align="center" label="小程序名称" prop="name"></el-table-column>
      <el-table-column align="center" header-align="center" label="小程序AppID" prop="app_id"></el-table-column>
      <el-table-column align="center" header-align="center" label="小程序二维码" prop="qr_code">
        <template slot-scope="scope">
          <span @click="handlePictureCardPreview('https://images.weserv.nl/?url=' + scope.row.qr_code)"><img :src="'https://images.weserv.nl/?url=' + scope.row.qr_code" alt="" width="100" height="100" /></span>
        </template>
      </el-table-column>
      <el-table-column align="center" header-align="center" label="创建人" prop="created_user"> </el-table-column>
      <el-table-column align="center" header-align="center" label="创建时间" prop="created_time" width="200"></el-table-column>
      <el-table-column align="center" fixed="right" header-align="center" label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="addOrUpdateHandle(scope.row)" plain type="primary">编辑</el-button>
          <el-button size="mini" @click="handleDel(scope.row)" plain type="danger">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      small
      :current-page="pageIndex"
      :page-size="pageSize"
      :page-sizes="[10, 20, 50, 100]"
      :total="totalPage"
      @current-change="currentChangeHandle"
      @size-change="sizeChangeHandle"
      layout="total, sizes, prev, pager, next, jumper"
      style="margin-top: 10px; text-align: right"
    ></el-pagination>

    <!-- 图片大图 -->
    <el-dialog :visible.sync="VisibleIMg">
      <img width="100%" :src="dialogImageUrl" alt="" />
    </el-dialog>
    <!-- 弹窗, 新增 / 修改 -->
    <popup @refreshList="getDataList" ref="popup"></popup>
    <!-- 删除前通过发送验证码检验身份，此为验证码弹框 -->
  </div>
</template>
<script>
import popup from './popup'
import { appletListAPI, appletDel } from '@/api/wxapplet/index.js'
export default {
  components: { popup },
  data() {
    return {
      VisibleIMg: false, //大图展示
      dialogImageUrl: '', //大图图片
      pageIndex: 1, //页数
      pageSize: 20, //当前页面条数
      totalPage: 0, //页面总条数
      searchForm: {
        //筛选数据
        name: '',
        created_user: '',
        date: [],
      },
      dataListLoading: false,
      dataList: [], //table数据
      multipleSelection: [], //选中的数据
    }
  },
  mounted() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      const info = Object.assign({ pageNumber: this.pageIndex, pageSize: this.pageSize }, this.searchForm)
      if (info.date.length) {
        info.start_time = info.date[0]
        info.end_time = info.date[1]
        delete info.date
      }

      const formData = new FormData()
      for (let key in info) {
        formData.set(key, info[key])
      }

      //列表数据获取
      appletListAPI(formData)
        .then((res) => {
          this.dataList = res.result.items
          this.totalPage = res.result.total
          this.dataListLoading = false
        })
        .catch((e) => {
          this.dataList = []
          this.totalPage = 0
          this.dataListLoading = false
        })
    },
    //重置
    refDataList() {
      this.searchForm = {
        name: '',
        created_user: '',
        date: [],
      }
      this.getDataList()
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },

    // 更新值
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 新增 / 修改
    addOrUpdateHandle(row) {
      this.$refs.popup.open(row)
    },
    // 删除table
    handleDel(row) {
      if (row) {
        // 单个删除
        var info = Object.assign({ ids: [row.id] })
      } else {
        // 批量删除
        var info = { ids: [] }
        for (let key in this.multipleSelection) {
          info.ids.push(this.multipleSelection[key].id)
        }
      }

      if (info.ids.length <= 0) return this.$message.error('请至少选择一个')

      const formData = new FormData()
      for (let key in info) {
        formData.set(key, info[key])
      }
      this.$confirm('确认删除')
        .then((_) => {
          appletDel(formData)
            .then((res) => {
              if (res.code == 200) {
                this.$message.success('删除成功')
                this.getDataList()
              }
            })
            .catch((e) => {
              this.$message.error('删除失败')
            })
        })
        .catch((_) => {})
    },
    // 图片click
    handlePictureCardPreview(url) {
      this.dialogImageUrl = url
      this.VisibleIMg = true
    },
  },
}
</script>
<style lang="scss" scoped></style>
